<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermission(0, 1)">添加权限</el-button>
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <!-- <el-button v-if="row.type === 1" size="mini" type="text" @click="doAdd(row.pid, 1)">添加</el-button> -->
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addPermission(row.id, 2)">添加</el-button>
            <!-- <el-button size="mini" type="text" @click="doEdit(row.id)">编辑</el-button> -->
            <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>
            <!-- <el-button size="mini" type="text" @click="doDel">删除</el-button> -->
            <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="`${showText}权限点`" :visible="showDialog" @close="hClose">
      <el-form ref="formData" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" autocomplete="off" />

        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" autocomplete="off" />

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hClose">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addPermission, delPermission, getPermissionDetail, getPermissionList, updatePermission } from '@/api/permission'
import { arr2tree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      // isEdit: false,
      showDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认
      },
      rules: {

        // name: [
        //   { required: true, message: '请输入权限名称', trigger: 'blur' },
        //   { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        // ],
        // code: [
        //   { required: true, message: '请输入权限标识', trigger: 'blur' },
        //   { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        // ]
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]

      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionList()// 将列表数据转化成树形结构
      console.log(res)
      this.list = arr2tree(res.data, 0) // 将列表数据转化成树形结构
    },
    // btnOK() {
    //   this.$refs.formData.validate(isOK => {
    //     if (isOK) {
    //       this.formData.pid ? updatePermission(this.formData) : addPermission(this.formData)
    //     }
    //   })
    // },
    // async doAdd(pid, type) {
    //   this.showDialog = true
    //   this.formData.pid = pid
    //   this.formData.type = type
    //   await addPermission(this.formData)
    //   this.$message.success('添加成功')
    //   this.getPermissionList()
    //   this.close()
    //   // this.$emit('updateManager')
    // },
    btnOK() {
      this.$refs.formData.validate().then(() => {
        if (this.formData.id) {
          return updatePermission(this.formData)
        }
        return addPermission(this.formData)
      }).then(() => {
        //  提示消息
        this.$message.success('新增成功')
        this.getPermissionList()
        this.showDialog = false
      })
    },
    addPermission(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    // async doEdit(id) {
    //   this.showDialog = true
    //   this.formData = await getPermissionDetail(id)
    //   // this.$emit('updateDepartment')
    //   // 提示消息
    //   this.$message.success('编辑成功')
    //   this.getPermissionList()
    //   this.close()
    // },
    async editPermission(id) {
      // 根据获取id获取详情
      const res = await getPermissionDetail(id)
      console.log(res)
      this.formData = res.data
      this.showDialog = true
    },
    async delPermission(id) {
      // try {
      // await this.$confirm('确定要删除该数据吗')
      await delPermission(id)
      this.$message.success('删除成功')
      this.getPermissionList()
      // this.showDialog = false
      // } catch (error) {
      //   console.log(error)
      // }
    },
    hClose() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认
      }
      this.$refs.formData.resetFields() // 重置表单
      // this.$emit('update:showDialog', false)
      this.showDialog = false
    }
  }

}
</script>
